const app = getApp()
const api = require("../../utils/api");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid : "",
    code : ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.autoValidateAction();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  loginAction : function(){
    let openid = app.globalData.openid;
    console.log(openid); // 输出
    wx.setClipboardData({
      data: openid, // 需要复制的文本内容
      success(res) {
        let content = "您的授权信息是 " + openid + " , 已复制到粘贴板中，你可以粘贴以便发送"
        wx.showModal({
          content: content,
        })
      },
      fail(err) {
        wx.showToast({
          title: '复制失败',
          icon: 'none'
        })
        console.error('复制失败:', err)
      }
    })
  },

  autoValidateAction : async function(){
    const res = await api.validateUser(app.globalData.openid, app.globalData.code)
    console.log(res)
    if (res == 1) {
      console.log("自动跳转")
      wx.navigateTo({
        url: '/pages/draw/list',
      })
    }
  },

  validateAction : async function(){
    const res = await api.validateUser(app.globalData.openid, this.data.code)
    console.log(res)
    if (res == 1) {
      app.globalData.code = this.data.code
      app.save()
      wx.navigateTo({
        url: '/pages/draw/list',
      })
    } else {
      wx.showToast({
        title: '验证失败',
        icon: "error"
      })
    }
  }
})